/**
 * Created by jiaaobo on 16/11/18.
 */
import Vue from 'vue';
import Mint from  "mint-ui";
import 'mint-ui/lib/style.css'
import VueRouter from  "vue-router";
import Filter from "./filter";
import VueTouch  from  'vue-touch';
import FastClick from  "../lib/event/fastclick";
import 'whatwg-fetch'
import Util from  "util";


//使用路由
Vue.use(VueRouter);
//使用MintUI
Vue.use(Mint);
//开启debug模式
Vue.config.debug = true;

Vue.use(VueTouch);


//导入过滤器
Filter();

FastClick.attach(document.body, {});

//事件总栈
var bus = new Vue();


/**
 * 路由映射表
 * @type {*[]}
 */
const routes = [
  {
    path : "/tab",
    component: require("../biz/tab/view/Tab")
  },

  {
    //帮助列表
    path : "/help/helpDoc",
    component  : require("../biz/help/view/HelpList"),
    meta: { scrollToTop: true }
  },

  {
    //帮助详情
    path : "/help/helpDetail/:id",
    component  : require("../biz/help/view/HelpDetail"),
    name : "helpDetail",
    meta: { scrollToTop: true }
  },

  {
    //开门结果
    path : "/order/orderResult",
    name: 'orderResult',
    component  : require("../biz/order/view/OrderResult"),
    meta: { scrollToTop: true }
  },

  {
    //下单
    path : "/order/add",
    component  : require("../biz/order/view/AddOrder"),
    meta: { scrollToTop: true }
  },

  {
    //商店详情
    path : "/store/detail/:storeId",
    name : "storeDetail",
    component  : require("../biz/store/view/StoreDetail"),
    meta: { scrollToTop: true }
  },

  {
    //我的预约
    path : "/order/my",
    component  : require("../biz/order/view/MyOrder"),
    meta: { scrollToTop: true }
  },


  {
    //预约详情
    path : "/order/detail/:id",
    component  : require("../biz/order/view/orderDetail"),
    meta: { scrollToTop: true }
  },

  {
    //优惠券列表
    path : "/voucher/list/:type",
    component  : require("../biz/voucher/view/VoucherList"),
    meta: { scrollToTop: true }
  },

  {
    //兑换优惠券
    path : "/voucher/exchange",
    component  : require("../biz/voucher/view/Exchange"),
    meta: { scrollToTop: true }
  },

  {
    //绑定用户
    path : "/auth/bindUser",
    component  : require("../biz/auth/view/BindUser"),
    meta: { scrollToTop: true }
  },

  {
    //用户详情
    path : "/me/update",
    component  : require("../biz/me/view/UpdateUserInfo"),
    meta: { scrollToTop: true }
  },
  {
    //排行榜
    path : "/me/ranking",
    component  : require("../biz/me/view/Ranking"),
    meta: { scrollToTop: true }
  },
  {
    //邀请用户
    path : "/market/invitation",
    name : "invitationUser",
    component  : require("../biz/market/view/InvitationUser"),
    meta: { scrollToTop: true }
  },
  {
    //share
    path : "/market/share/:id",
    component  : require("../biz/market/view/InvitationShare"),
    meta: { scrollToTop: true }
  },
  {
    //排行榜
    path : "/me/ranking",
    component  : require("../biz/me/view/Ranking"),
    meta: { scrollToTop: true }
  },
  //默认路径
  {path: '*', redirect: '/tab'}
];

window.HOST =  "http://www.7vii.cn/vii";
// window.HOST =  "http://172.16.222.129:8080/vii";

const scrollBehavior = (to, from, savedPosition) => {
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    return savedPosition
  } else {
    const position = {}
    // new navigation.
    // scroll to anchor by returning the selector
    if (to.hash) {
      position.selector = to.hash
    }
    // check if any matched route config has meta that requires scrolling to top
    if (to.matched.some(m => m.meta.scrollToTop)) {
      // cords will be used if no selector is provided,
      // or if the selector didn't match any element.
      position.x = 0
      position.y = 0
    }
    // if the returned position is falsy or an empty object,
    // will retain current scroll position.
    return position
  }
}

const router = new VueRouter({
  routes : routes,
  scrollBehavior
});


/**
 * 路由钩子
 */
router.beforeEach((to, from, next) => {
  console.log("before..",to);
  console.log("before..",from);

  //商店详情 邀请用户 帮助
  switch (to.name && to.name){
    case  'helpDetail':
    case  'storeDetail':
    case  'invitationUser':
      Util.WX.toggleMenu(true,'spread');
      break;
    default:
      Util.WX.toggleMenu(false);
      break;
  }
  next();
});

window.getRouter = function(){
  return router;
}

window.getEventBus = function(){
  return bus;
}

window.getAbsHost = function(){
  if(window.location.host.indexOf('www.7vii.cn') != -1){
    return window.location.host + "/vii-web"
  }
  else{
    return window.location.host;
  }
}

window.forwardAuthPage = function(){
  let authUrl = HOST+"/wxAuth/authPage.do";
  window.location.href = authUrl;
}





module.exports = {
  router : router
}
